# -*- coding:utf-8 -*-
# @Time: 2020/9/7 15:21
# @Author: gupeng
# @File: article.py
# @Desc: 文章--测试mset,mget,strlen,getrange
from time import time  # time函数用于获取当前时间的时间戳


class Article:
    def __init__(self, client, article_id):
        self.client = client
        self.id = str(article_id)
        self.title_key = 'article::' + self.id + '::title'
        self.content_key = 'article::' + self.id + '::content'
        self.author_key = 'article::' + self.id + '::author'
        self.create_at_key = 'article::' + self.id + '::create_at'

    def create(self, title, content, author):
        """
        创建一篇新的文章
        :param title: 标题
        :param content: 内容
        :param author: 作者
        :return: 创建成功返回True，否则因文章已经存在创建失败返回False
        """
        article_data = {
            self.title_key: title,
            self.content_key: content,
            self.author_key: author,
            self.create_at_key: time()
        }
        self.client.mset(article_data)

    def get(self):
        """
        返回id对应的文章信息
        :return: 文章信息
        """
        result = self.client.mget(self.title_key,
                                  self.content_key,
                                  self.author_key,
                                  self.create_at_key)
        return {'id': self.id, 'title': result[0],
                'content': result[1], 'author': result[2],
                'create_at': result[3]}

    def update(self, title=None, content=None, author=None):
        """
        对文章的各项信息进行更新
        :param title: 标题
        :param content: 内容
        :param author: 作者
        :return: 更新成功返回True，失败时返回False
        """
        article_data = {}
        if title is not None:
            article_data[self.title_key] = title
        if content is not None:
            article_data[self.content_key] = content
        if author is not None:
            article_data[self.author_key] = author
        return self.client.mset(article_data)

    def get_content_len(self):
        """
        获取文章内容的长度
        :return: 文章内容的长度
        """
        return self.client.strlen(self.content_key)

    def get_content_preview(self, preview_len):
        """
        获取指定长度的文章内容的预览
        :param preview_len: 内容长度
        :return: 指定长度的文章内容
        """
        start_index = 0
        end_index = preview_len - 1
        return self.client.getrange(self.content_key, start_index, end_index)
